#include "completers.h"
#include <QStringListModel>
#include <QSettings>
#include <QPointer>


QStringListModel* Completers::nameModel(QObject* parent) {
    static QPointer<QStringListModel> retval;
    QSettings s;
    QMap<QString, QVariant> m = map();
    if (retval == 0) {
        retval = new QStringListModel(m.keys(), parent);
    }
    else {
        retval->setStringList(m.keys());
    }
    return retval.data();
}

QStringListModel* Completers::urlModel(QObject *parent) {
    static QPointer<QStringListModel> retval;
    QMap<QString, QVariant> m = map();
    QStringList urls;
    foreach (QVariant v, m.values()) {
        urls << v.toString();
    }
    if (retval == 0) {
        retval = new QStringListModel(urls, parent);
    }
    return retval;
}

QMap<QString, QVariant> Completers::map()  {
    QSettings s;
    return  s.value("history/map").toMap();
}

void Completers::add(QString name, QString url) {
    QSettings s;
    QMap<QString, QVariant> m = map();
    m[name]=url;
    s.setValue("history/map", m);
}

